//
//  JZYWCViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "JZYWCViewController.h"

#import "YJDCell.h"
#import "JZYWCDetViewController.h"
@interface JZYWCViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UIImageView *imageView;
    UILabel *labTit;
    NSInteger pageNo;
}

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *array;

@end

@implementation JZYWCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _array = [NSMutableArray array];
    [self showNav:@"已完成"];
    pageNo = 1;
    
    [self loadUI];
    [self loadRefresh];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"available"]];
    imageView.hidden = YES;
    [self.view addSubview:imageView];
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
    
    labTit = [[UILabel alloc] init];
    labTit.text = @"暂无数据...";
    labTit.hidden = YES;
    labTit.font = [UIFont systemFontOfSize:15.0];
    labTit.textColor = [UIColor lightGrayColor];
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(imageView.bottom).offset(8);
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    YJDCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YJDCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.btnjd.hidden = YES;
    cell.labPs.hidden = YES;
//    cell.labPrice.hidden = YES;
    
    NSDictionary *itemDic = [_array objectAtIndex:indexPath.row];
    [cell.labDDH setText:[NSString stringWithFormat:@"订单号：%@",[itemDic objectForKey:@"ordernumber"]]];
    [cell.labSth setText:[itemDic objectForKey:@"ordername"]];
    [cell.labDate setText:[self getNewDate:[itemDic objectForKey:@"addtime"]]];
    cell.labPrice.text = [NSString stringWithFormat:@"%@%@",[itemDic objectForKey:@"ordermoney"],[itemDic objectForKey:@"timetype"]];
    
    NSString *orderstate = [NSString stringWithFormat:@"%li",[[itemDic objectForKey:@"orderstate"] integerValue]];
    if ([orderstate integerValue] == 2) {
        cell.labState.text = @"等接单";
        cell.labState.textColor = [UIColor lightGrayColor];
    }else if ([orderstate integerValue] == 3){
        cell.labState.text = @"正在服务中";
        cell.labState.textColor = [UIColor lightGrayColor];
    }else if ([orderstate integerValue] == 4){
        cell.labState.text = @"订单已完成";
        cell.labState.textColor = [UIColor lightGrayColor];
    }else if ([orderstate integerValue] == 6){
        cell.labState.text = @"订单取消确认中";
        cell.labState.textColor = [UIColor lightGrayColor];
    }else if ([orderstate integerValue] == 7){
        cell.labState.text = @"已取消";
        cell.labState.textColor = [UIColor lightGrayColor];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _array[indexPath.row];
    JZYWCDetViewController *ywc = [[JZYWCDetViewController alloc] init];
    NSString *orderid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    ywc.orderId = orderid;
    ywc.merid = _merid;
    [self.navigationController pushViewController:ywc animated:YES];
}

//刷新控件
- (void)loadRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //
    [self.tableView.mj_header beginRefreshing];
    //根据位置设置半透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}

//家政已完成
- (void)loadData{
    [_tableView.mj_footer endRefreshing];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_merid forKey:@"merchantid"];
    [parmas setValue:@"1" forKey:@"p"];
    NSLog(@"parmas:%@",parmas);
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wchousekeepingorder",urlHead];
    NSLog(@"url:%@",url);
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"已完成res:%@",responseObject);
        NSString *error = [responseObject objectForKey:@"error"];
        NSString *mssage = [responseObject objectForKey:@"mssage"];
        NSMutableArray *result = [responseObject objectForKey:@"result"];
        if ([error integerValue] == 200 && [result isKindOfClass:[NSArray class]]) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            _array = [arr mutableCopy];
            [self.tableView reloadData];
            
            _tableView.hidden = NO;
            imageView.hidden = YES;
            labTit.hidden = YES;
        }else{
            _tableView.hidden = YES;
            imageView.hidden = NO;
            labTit.hidden = NO;
            
        }
        [LICGToast showWithText:mssage];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData{
    [_tableView.mj_header endRefreshing];
    pageNo++;
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *p = [NSString stringWithFormat:@"%li",pageNo];
    [parmas setObject:p forKey:@"p"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wchousekeepingorder",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"苦力已完成res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            [_array addObjectsFromArray:arr];
        }
        [_tableView reloadData];
        
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_footer endRefreshing];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
